class FriendController < ApplicationController
  before_filter :auth
  def index
	@me=User.find(session[:user_id])
  end
  def usermod
	@me=User.find(session[:user_id])
	@func=-101
	user=User.find_by_email(params[:email].strip)
	if (user.nil? || user.id==@me.id)
		if(request.post? && @me.password==params[:oldpassword])
			@me.email=params[:email].strip 
			@me.adsense_id=params[:adsense]
			@me.is_public=params[:is_public].to_i
			@me.notifyme=params[:notifyme].to_i
		    @me.login=params[:nickname]
		    @me.password=params[:password] unless params[:password].nil? ||  params[:password].blank?
		    @me.interests=params[:des]
			@me.is_guest=0
			@me.save
			@func=-102
		end
	else
		@func=-103
	end
  	
  	render :action=>'netop',:layout => false 
  end
  def search
  	@action='search'
  	@func=100
  	@manager=NetManager.new(session[:user_id])
  	if params[:catelog]=='hash'
	    user=ViewUserCount.find_by_hashkey(params[:des])
	    unless user.nil?
	    	users=[user]
	    end
  	else
  		@count=User.count(:all,:conditions=>["is_public=1 and interests LIKE :interests",{:interests=>'%'+params[:des]+'%'}])
  		@page=params[:page].to_i
	    users=ViewUserCount.find(:all,:order => "usercount DESC",:limit =>10,:offset=>@page*10, :conditions=>["is_public=1 and interests LIKE :interests",{:interests=>'%'+params[:des]+'%'}])
  	end
  	@results=((users.nil?)?[]:users)
  	render :action=>'netop',:layout => false 
  end
  def invite
  	@func=-1
  	if (request.post? && (!params[:email].nil?))
  		emaila=params[:email].split(",")
  		emaila.each do |email|
	    	invite = Invitation.find(:first,:conditions=>["user_id=:user_id and email=:email",{:user_id=>session[:user_id],:email=>email}])   
	    	if invite.nil?
		    	require 'digest/md5'
	    		invite = Invitation.new
	    		invite.user_id=session[:user_id]
	    		invite.email=email
	    		invite.create_at = Time.now
	    		invite.hashkey=(Digest::MD5).hexdigest(email+session.session_id.to_s).to_s
	    		invite.save 
	    	end
	    	unless invite.friend_id>0
				@me=User.find(session[:user_id])
  				KerjoMailer.deliver_invite(@me.email,email,(request.protocol << request.host_with_port)+"/index?inviteid="+invite.hashkey)
  			end
  		end
		@func=-2
  	end
  	render :action=>'netop',:layout => false 
  end
  
  def mynet
  	@action='mynet'
  	@manager=NetManager.new(session[:user_id])
  	@func=0
    @count=User.count(:all,:conditions=>["uc.user_id=:user_id ",{:user_id=>session[:user_id]}],:joins => " inner join user_connections as uc on `users`.id = uc.friend_id")
  	@page=params[:page].to_i
    users=ViewUserCount.find(:all,:order => "usercount DESC",:limit =>10,:offset=>@page*10,:conditions=>["uc.user_id=:user_id ",{:user_id=>session[:user_id]}],:joins => " inner join user_connections as uc on `view_user_counts`.id = uc.friend_id")
    @results=((users.nil?)?[]:users)
  	render :action=>'netop',:layout => false 
  end
  def topestnewst
  	@manager=NetManager.new(session[:user_id])
  	@func=0
  	unless (params[:friend_id].nil?) || (params[:type].nil?)
	  	case params[:type]
  		when 'add'
		  	@manager.add_friend_with_notify(params[:friend_id],request)
		when 'remove'
		  	@manager.remove_friend(params[:friend_id])
		when 'join'
		  	@manager.join_net(params[:friend_id])
		when 'leave'
		  	@manager.leave_net(params[:friend_id])
		end
  	end  
    @topest_users=ViewUserCount.find(:all,:order => "usercount DESC",:limit =>5, :conditions=>["is_public=1 AND usercount>0 AND LENGTH(interests)>12"])
    @newest_users=ViewUserCount.find(:all,:order => "id DESC",:limit =>5, :conditions=>["is_public=1  AND usercount>0 AND LENGTH(interests)>12"])
  	render :action=>'netop',:layout => false 
  end
  def browse
  	@action='browse'
  	@manager=NetManager.new(session[:user_id])
  	@func=3
    @count=User.count(:all,:conditions=>'is_public=1')
  	@page=params[:page].to_i
    users=ViewUserCount.find(:all,:conditions=>'is_public=1',:order => "usercount DESC",:limit =>10,:offset=>@page*10)
    @results=((users.nil?)?[]:users)
  	render :action=>'netop',:layout => false 
  end
  def friend
  	@action='friend'
  	@manager=NetManager.new(session[:user_id])
  	@func=1
    @count=User.count(:all,:conditions=>["fr.user_id=:user_id ",{:user_id=>session[:user_id]}],:joins => " inner join friends as fr on `users`.id = fr.friend_id")
  	@page=params[:page].to_i
    users=ViewUserCount.find(:all,:order => "usercount DESC",:limit =>10,:offset=>@page*10,:conditions=>["fr.user_id=:user_id ",{:user_id=>session[:user_id]}],:joins => " inner join friends as fr on `view_user_counts`.id = fr.friend_id")
    @results=((users.nil?)?[]:users)
  	render :action=>'netop',:layout => false 
  end
  def top10
  	@manager=NetManager.new(session[:user_id])
  	@func=2
    users=ViewUserCount.find(:all,:conditions=>'is_public=1',:order => "usercount DESC",:limit =>10)
    @results=((users.nil?)?[]:users)
    @count=@results.size
  	render :action=>'netop',:layout => false 
  end
  def netop
  	unless (params[:friend_id].nil?) || (params[:type].nil?)
	  	@manager=NetManager.new(session[:user_id])
	  	case params[:type]
  		when 'add'
		  	@manager.add_friend_with_notify(params[:friend_id],request)
		when 'remove'
		  	@manager.remove_friend(params[:friend_id])
		when 'join'
		  	@manager.join_net(params[:friend_id])
		when 'leave'
		  	@manager.leave_net(params[:friend_id])
		end
		@result=ViewUserCount.find(params[:friend_id])
  	end
	render :layout => false 
  end
end